//
//  QBillingDetailVC.m
//  QoilPublic
//
//  Created by zhuang on 16/11/10.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QBillingDetailVC.h"
#import "QBillingContentVC.h"
#import "QChargeBookDB.h"
@interface QBillingDetailVC ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIScrollView *sScrollView;

@property(nonatomic,strong)UIScrollView *bScrollView;

@property(nonatomic,strong)NSMutableArray *btnArr;

@property(nonatomic,strong)UIView *selectView;

@end

@implementation QBillingDetailVC{

    CGFloat W;
    
    NSMutableArray *_yearArr;
    NSMutableArray *_monthArr;
    NSMutableArray *_monthLengthArr;
}

- (id)init{
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"账单明细";
    self.automaticallyAdjustsScrollViewInsets = NO;
    W = kScreenWidth/5;
    [self createData];

    self.sScrollView.contentSize = CGSizeMake(_yearArr.count * W, 0);
    [self.view addSubview:self.sScrollView];
    
    self.bScrollView.contentSize = CGSizeMake(_yearArr.count * kScreenWidth, 0);
    [self.view addSubview:self.bScrollView];

    self.selectView.frame = CGRectMake(0, 43, W, 2);
    [self.sScrollView addSubview:self.selectView];
    [self addButton];
    [self addController];
    
    QBillingContentVC *vc = [self.childViewControllers firstObject];
    vc.view.frame = self.bScrollView.bounds;
    vc.view.backgroundColor = [UIColor redColor];
    [self.bScrollView addSubview:vc.view];
}

- (void)createData{
    
    _yearArr = [NSMutableArray array];
    _monthArr = [NSMutableArray array];
    _monthLengthArr = [NSMutableArray array];

    NSMutableArray *yarr = [NSMutableArray array];//年
    NSMutableArray *marr = [NSMutableArray array];//月
    NSMutableArray *mLarr = [NSMutableArray array];//一个月有多少天
    NSDate *date = [NSDate date];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    for(int i = 0; i> -12; i--){
        dateComponents.month = i;
        NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];//前十二个月的时间
        NSInteger monthL = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit: NSCalendarUnitMonth forDate:newDate].length;
        [mLarr addObject:[NSString stringWithFormat:@"%ld",(long)monthL]];//每个月有多少天
        NSDateFormatter *datefot = [[NSDateFormatter alloc] init];
        datefot.dateFormat = @"YYYY/MM-dd HH:mm";
        [QChargeBookDB formatDate:[datefot stringFromDate:newDate] successBlock:^(NSString *y, NSString *m, NSString *d, NSString *t) {
            CLog(@"年==%@ 月==%@",y,m);
            [yarr addObject:y];
            [marr addObject:m];

        }];
    }
    
   [_yearArr addObjectsFromArray:[[yarr reverseObjectEnumerator] allObjects]];
   [_monthArr addObjectsFromArray:[[marr reverseObjectEnumerator] allObjects]];
    [_monthLengthArr addObjectsFromArray:[[mLarr reverseObjectEnumerator] allObjects]];
   [_yearArr addObject:@"全部"];
   [_monthArr addObject:@"全部"];
   [_monthLengthArr addObject:@"全部"];
    
}

- (void)addController{
    for (int i=0 ; i<_yearArr.count ;i++){
        QBillingContentVC *vc1 = [[QBillingContentVC alloc] init];
        vc1.yearArr = _yearArr;
        vc1.monthArr = _monthArr;
        vc1.monthLengthArr = _monthLengthArr;
        vc1.index = i;
        [self addChildViewController:vc1];
    }
}

- (void)addButton{

    for (int i=0; i<_yearArr.count; ++i) {
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i*W, 0, W, 40)];
        if (i == _yearArr.count-1){
            [button setTitle:_yearArr[i] forState:UIControlStateNormal];
        }else{
            [button setTitle:[NSString stringWithFormat:@"%@-%@",_yearArr[i],_monthArr[i]] forState:UIControlStateNormal];
        }
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        button.tag = i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.sScrollView addSubview:button];
        if(button.tag == 0){
            [button setTitleColor:[QColorUtility defaultBlueColor] forState:UIControlStateNormal];
        }else{
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
        UIView *grayV = [[UIView alloc] initWithFrame:CGRectMake(button.right, 8, 1, 29)];
        grayV.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self.sScrollView addSubview:grayV];
        [self.btnArr addObject:button];
    }
}

- (void)buttonAction:(UIButton *)btn{

    CGFloat px = btn.tag * kScreenWidth;
    CGFloat py = self.bScrollView.contentOffset.y;
    [self.bScrollView setContentOffset:CGPointMake(px, py) animated:YES];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    if(scrollView.tag == 2){
        NSUInteger index = scrollView.contentOffset.x/kScreenWidth;
        [self setupButtonColor:index];
        [UIView animateWithDuration:0.23 animations:^{
            _selectView.frame = CGRectMake(index*W, 43, W, 2);
        }];
        QBillingContentVC *vc = (QBillingContentVC *)self.childViewControllers[index];
        UIButton *btn = _btnArr[index];
        CGFloat offsetx = btn.center.x - kScreenWidth * 0.5;
        CGFloat offsetMax = self.sScrollView.contentSize.width - kScreenWidth;
        if(offsetx<0){
            offsetx = 0;
        }else if (offsetx > offsetMax){
            offsetx = offsetMax;
        }
        CGPoint offset = CGPointMake(offsetx, self.sScrollView.contentOffset.y);
        [self.sScrollView setContentOffset:offset animated:YES];
        
        if(vc.view.superview) return;
        vc.view.frame = scrollView.bounds;
        vc.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self.bScrollView addSubview:vc.view];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if(scrollView.tag == 2){
       [self scrollViewDidEndScrollingAnimation:scrollView];
    }
}

//滑动调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(scrollView.tag == 1){
        CGFloat scrollViewLeftX = scrollView.contentOffset.x;
        if(scrollViewLeftX < 0){
            self.sScrollView.scrollEnabled = NO;
        }else{
            self.sScrollView.scrollEnabled = YES;
        }
    }else if (scrollView.tag == 2){
        CGFloat index = (scrollView.contentOffset.x/self.bScrollView.frame.size.width);
        _selectView.frame = CGRectMake(W * index , 43, W, 2);
        CGFloat scrollViewLeftX = scrollView.contentOffset.x;
        if(scrollViewLeftX < 0){
            self.bScrollView.scrollEnabled = NO;
        }else{
            self.bScrollView.scrollEnabled = YES;
        }
    }
}

//设置button颜色
- (void)setupButtonColor:(NSInteger)index{
    
    for (UIButton *btn in _btnArr) {
        if(btn.tag == index){
            [btn setTitleColor:[QColorUtility defaultBlueColor] forState:UIControlStateNormal];
        }else{
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
    }
}

#pragma mark --- get/set
- (UIScrollView *)sScrollView{
    if (!_sScrollView) {
        _sScrollView = [[UIScrollView alloc] init];
        _sScrollView.showsHorizontalScrollIndicator = NO;
        _sScrollView.showsVerticalScrollIndicator = NO;
        _sScrollView.scrollsToTop = NO;
        _sScrollView.tag = 1;
        _sScrollView.delegate = self;
        _sScrollView.frame = CGRectMake(0, kNavHeight+kStatusHeight, kScreenWidth, 45);

    }
    return _sScrollView;
}

//#pragma mark --- get/set
- (UIScrollView *)bScrollView{
    if (!_bScrollView) {
        _bScrollView = [[UIScrollView alloc] init];
        _bScrollView.showsHorizontalScrollIndicator = NO;
        _bScrollView.showsVerticalScrollIndicator = NO;
        _bScrollView.pagingEnabled = YES;
        _bScrollView.delegate = self;
        _bScrollView.tag = 2;
        _bScrollView.frame = CGRectMake(0,kNavHeight+kStatusHeight+45, kScreenWidth,kScreenHeight-kNavHeight-kStatusHeight-45);
        
    }
    return _bScrollView;
}

- (UIView *)selectView{
    if (!_selectView) {
        _selectView = [[UIView alloc] init];
        _selectView.backgroundColor = [QColorUtility defaultBlueColor];
    }
    return _selectView;
}


- (NSMutableArray *)btnArr{
    if(!_btnArr){
        _btnArr = [NSMutableArray array];
    }
    return _btnArr;
}


@end
